from enum import Enum

import uvicorn
from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html, get_redoc_html
from starlette.staticfiles import StaticFiles

# 创建FastAPI实例，禁用掉FastAPI会自动生成Swagger UI和Redoc文档
app = FastAPI(docs_url=None, redoc_url=None)

# 静态文件挂载（路由与静态文件映射）
app.mount("/static", StaticFiles(directory="../statics"), name="static")


# 重新提供一个Swagger UI，相当于我们自己重写了这么一个路由
@app.get("/docs", include_in_schema=False)
def custom_swagger_ui():
    return get_swagger_ui_html(
        openapi_url = "/openapi.json",
        title = "Swagger UI for FastAPI",
        swagger_js_url="/static/swagger-ui-bundle.min.js",
        swagger_css_url="/static/swagger-ui.min.css"
    )
@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
    return get_swagger_ui_oauth2_redirect_html()

# 这里与Swagger UI一样，重新提供Redoc
@app.get("/redoc", include_in_schema=False)
def custom_redoc():
    return get_redoc_html(
        openapi_url = "/openapi.json",
        title = "Redoc for FastAPI",
        redoc_js_url="/static/redoc.standalone.js"
    )

items_db = [
    {"item_name": "手机"},
    {"item_name": "平板"},
    {"item_name": "电视"},
    {"item_name": "PAD"},
    {"item_name": "服饰"},
    {"item_name": "鞋帽"},
    {"item_name": "电脑"},
    {"item_name": "游戏机"},
    {"item_name": "路由器"},
    {"item_name": "智能音箱"},
    {"item_name": "智能家居"},
    {"item_name": "智能设备"},
    {"item_name": "存储设备"},
]
@app.get("/items")
async def read_item(page_index: int = 1, page_size: int = 10):
    start_index = (page_index - 1) * page_size
    end_index = start_index + page_size
    return items_db[start_index:end_index]

@app.get("/items/{item_id}")
# 这里item_id是路径参数，不可以有默认值
# q是查询参数，可以有默认值，这里的默认值是None
async def read_item(item_id: int, q: str | None = None):
    if q:
        return {"item_id": item_id, "q": q}
    else:
        return {"item_id": item_id}


@app.get("/students/{student_id}")
async def read_student(student_id: int,q: str | None = None, is_active: bool = False):
    student = {
        "student_id": student_id
    }
    if q:
        student.update({"q": q})
    if not is_active:
        student.update({"is_active": is_active})
    return student

@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(
        user_id: int,item_id: int, q: str | None = None, is_active: bool = False
):
    item = {"item_id": item_id,"owner_id": user_id}
    if q:
        item.update({"q": q})
    if not is_active:
        item.update({"is_active": is_active})

    return item

@app.get("/orders/{order_id}")
# 这里order_no是查询参数，但是没有指定默认值所有它是必选的参数
async def read_order(order_id: int, order_no: str):
    return {"order_id": order_id, "order_no": order_no}


@app.get("/goods/{goods_id}")
async def read_goods(goods_id: int, goods_name: str, count: int = 1, warehouse: str | None = None):
    goods = {"goods_id": goods_id}
    if warehouse:
        goods.update({"warehouse": warehouse})
    return {"goods_name": goods_name, "count": count, **goods}


class Gender(str, Enum):
    male = "male"
    female = "female"

@app.get("/emps/{emp_id}")
async def read_emp(emp_id: int, emp_name: str, gender: Gender = Gender.male):
    emp = {"emp_id": emp_id}
    return {"emp_name": emp_name, "gender": gender, **emp}

if __name__ == '__main__':
    app.debug = True
    uvicorn.run("main:app", host="127.0.0.1", port=8002, reload=True)

